<%@ page import="java.util.jar.Attributes.Name"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Degree-university</title>
<link href="main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>

<%
	Applicant a = (Applicant) session.getAttribute("applicant");
	LinkedList<Degree> degrees = a.getDegrees();
	if(degrees == null)
	{
		degrees = new LinkedList<Degree>();
		a.setDegrees(degrees);
	}
	Degree degree = null;
	// To avoid adding duplicated degree when user reload the page
	if(session.getAttribute("degree") != null)
	{
		degree = (Degree)session.getAttribute("degree");
		session.removeAttribute("degree");
		degrees.add(degree);
	}
	degree = degrees.getLast();
	University university =  new University();
	Location loc = null;
	String location_entered =  null;
	String location_name = (String)request.getParameter("location_name");
	String location = (String)request.getParameter("location");
	String loc_id = (String)request.getParameter("location_id");
	String university_name = null;
	LinkedList<University> universities = null;
	DatabaseAccessObject dao = new DatabaseAccessObject();
	if (location_name != null && location == null){
		// User choose an existing location
		int location_id = Integer.parseInt(loc_id);		
		if(location_id != -1)
		{
			loc = new Location(location_id,location_name);
			//universities retrieved from the database
			universities = dao.getUniversitiesByLocation(location_name);
		}
		//User chooses a location he/she entered for a previous degree
		else
		{
			loc = new Location(location_id,location_name);
			universities = new LinkedList<University>();
			
		}
		/* find the universities this user has entered for this location 
		and append these universities to the list we have retrieved from database.	
		*/
		int id;
		Set<String> university_input_set = new HashSet<String>();
		for(int i = 0;i<degrees.size()-1;i++)
		{
			location_entered = degrees.get(i).getUniversity().getLocation().getName();
			//if the location of a previous degree is the same as the current one
			if(location_entered.equals(location_name))
			{
				university_name = degrees.get(i).getUniversity().getName();
				//find out whether the university of a previous degree already 
				// exists in the database
				id = dao.getUniversityIdByName(university_name,location_name);
				//if it doesn't exist and haven't been listed in the university list,
				//append it to the end of the university list
				if((id == -1)&&!university_input_set.contains(university_name))
				{
					university_input_set.add(university_name);
					universities.add(degrees.get(i).getUniversity());
				}
			}
		}
		university.setLocation(loc);
	}
	// User manually enter a location
	else {		
		loc = new Location(-1,location);
		int id = dao.getLocationIdByName(location);
		//if the user manually entered location already exists in the database
		if(id != -1)
		{
			//retrieve all the university in this location
			universities = dao.getUniversitiesByLocation(location);
		}
		else
		{
			universities = new LinkedList<University>();
		}
		Set<String> university_input_set = new HashSet<String>();
		int university_id;
		for(int i = 0;i<degrees.size()-1;i++)
		{
			//if the location of a previous degree is the same as the current one
			location_entered = degrees.get(i).getUniversity().getLocation().getName();
			if(location_entered.equals(location))
			{
				university_name = degrees.get(i).getUniversity().getName();
				//find out whether the university of a previous degree already 
				// exists in the database				
				id = dao.getUniversityIdByName(university_name,location);
				//if it doesn't exist and haven't been listed in the university list,
				//append it to the end of the university list
				if((id == -1)&&!university_input_set.contains(university_name))
				{
					university_input_set.add(university_name);
					universities.add(degrees.get(i).getUniversity());
				}
			}
		}
		university.setLocation(loc);
	}
	degree.setUniversity(university);
%>
<h1>Provide degrees - Choose University</h1>
	<% if (universities.size() == 0) { %>
	No any universities in this location
	<% } else { %>
	<table class="multicolumn">
		<tr>
			<% 
			int cols = 3;
			int itemsPerCol = universities.size() / cols;
			if (universities.size() % cols != 0) itemsPerCol += 1;
			for(int i=0; i<universities.size(); i++){ 
				if (i % itemsPerCol == 0)  out.println("<td style=\"vertical-align:top\">");
				out.println("<a class=\"list\" href=\"degree_discipline.jsp?univ_id=" + universities.get(i).getId() + 
						"&university_name=" +  universities.get(i).getName() + "\">" + universities.get(i).getName() + "</a>");
				if (i % itemsPerCol == itemsPerCol-1 || i==universities.size() -1 )  out.println("</td>");
			};
			%>
		</tr>
	</table>
	<% } %>
	<hr>
	<form name="form1" action="degree_discipline.jsp" method="post">
		<h2>If not found, input your university below:</h2>
		<input id = "university" type="text" name="university" value="" onkeyup = "universityCheck('<%= loc.getName() %>')">
		<input type="submit" name = "submit" value = "submit">
	</form>	
</body>
</html>